#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, k, a[10000007];
/*
3 2
8 1 9 8
*/
const int p = 1e9 + 7;


inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}

inline int read(){
	int sum=0,f=0;
	char ch=gc();
	for(;!isdigit(ch);ch=gc())f|=(ch=='-');
	for(;isdigit(ch);ch=gc()) sum=((sum<<1)+(sum<<3)+(ch^48));
	return f?-sum:sum;
}

namespace CR200J {
	long long qpow(long long a, long long b) {
		long long res = 1;
		while (b) {
			if (b & 1) res = res * a % p;
			a = a * a % p;
			b >>= 1;
		}
		return res;
	}
	int dp[5005];
	int dfs(int i) {
		if (i == 0) return 0;
		if (dp[i] != -1) return dp[i];
		int maxn = 0, ans = 1ll << 60;
		for (int r = i; r >= max(1ll, i - k + 1); --r) {
			maxn = max(maxn, a[r]);
			ans = min(ans, dfs(r - 1) + maxn);
		}
		return dp[i] = ans;
	}
	void main() {
		memset(dp, 255, sizeof(dp));
		int ans = 0;
		for (int i = 1; i <= n; ++i) {
			(ans += qpow(23, n - i) * dfs(i) % p) %= p;
		}
		cout << ans << "\n";
	}
}

namespace CR400AF {
	long long qpow(long long a, long long b) {
	long long res = 1;
		while (b) {
			if (b & 1) res = res * a % p;
			a = a * a % p;
			b >>= 1;
		}
		return res;
	}
	int dp[500005];
	struct SGT {
		#define ls(p) (p << 1)
		#define rs(p) (p << 1 | 1)
		int res[500005*4];
		void push_up(int i) {
			res[i] = max(res[ls(i)], res[rs(i)]);
		}
		void build(int i, int l, int r) {
			if (l == r) {
				res[i] = a[l];
				return;
			}
			int mid = (l + r) / 2;
			build(ls(i), l, mid);
			build(rs(i), mid + 1, r);
			push_up(i);
		}
		int query(int i, int l, int r, int ql, int qr) {
			if (l > qr || r < ql) return 0;
			if (l >= ql && r <= qr) return res[i];
			int mid = (l + r) / 2;
			return max(query(ls(i), l, mid, ql, qr), query(rs(i), mid + 1, r, ql, qr));
		}
	} t;

	int dfs(int i) {
		if (i == 0) return 0;
		if (dp[i] != -1) return dp[i];
		int ans = 1ll << 60;
		ans = min(ans, dfs(max(0ll, i - k)) + t.query(1, 1, n, i - k + 1, i));
		return dp[i] = ans;
	}
	void main() {
		memset(dp, 255, sizeof(dp));
		int ans = 0;
		t.build(1, 1, n);
		for (int i = 1; i <= n; ++i) {
			(ans += qpow(23, n - i) * dfs(i) % p) %= p;
		}
		cout << ans << "\n";
	}
}

namespace CR400BF {
	int dp[10000007];
	void main() {
		for (int i = 1; i <= n; ++i) {
			int maxn = 0, ans = 1ll << 60;
			for (int r = i; r >= max(1ll, i - k + 1); --r) {
				maxn = max(maxn, a[r]);
				ans = min(ans, dp[r - 1] + maxn);
			}
			dp[i] = ans;
		}
		int bs = 1, ans = 0;
		for (int i = n; i >= 1; --i) {
			(ans += bs * dp[i] % p) %= p;
			(bs *= 23) %= p;
		}
		cout << ans << "\n";
	}
}
signed main() {
	freopen("knight.in", "r", stdin); freopen("knight.out", "w", stdout);
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	n = read(); k = read();
	bool flg = 1;
	for (int i = 1; i <= n; ++i) a[i] = read();
	for (int i = 2; i <= n; ++i) {
		if (a[i] >= a[i - 1]);
		else flg = 0;
	}
	if (n <= 2000) {
		CR200J::main();
	} else if (flg) {
		CR400AF::main();
	} else {
		CR400BF::main();
	}
	return 0;
}
